package org.frankkie.hogerlagerdroid;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ScrollView;
import android.widget.TextView;
import java.io.PrintStream;

/**
 *
 * @author frankkie
 */
public class MainActivity extends Activity {

  public static EditText edit1;
  public static TextView tv2;
  public static int theNumber;
  public static int countGuess;
  public static Button bt1;
  public static ScrollView scroll;
  public static final int INSERT_ID = Menu.FIRST;

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case INSERT_ID:
        startGame();
        return true;
    }
    return super.onOptionsItemSelected(item);
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    boolean result = super.onCreateOptionsMenu(menu);
    menu.add(0, INSERT_ID, 0, R.string.menu_newgame);
    return result;
  }

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);
    //
    System.setErr(new PrintStream(new SystemOut("System.err")));
    System.setOut(new PrintStream(new SystemOut("System.out")));
    //
    try {
      bt1 = (Button) findViewById(R.id.bt1);
    } catch (Exception e) {
      e.printStackTrace();
    }
    tv2 = (TextView) findViewById(R.id.tv2);
    edit1 = (EditText) findViewById(R.id.edit1);
    scroll = (ScrollView) findViewById(R.id.scroll);
    try {

      ////////////HIER
      if (bt1 == null) {
        System.out.println("bt1==null !!!!!!!!!!");
      }

      bt1.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {
          checkTheNumber();
        }
      });
    } catch (Exception ex) {
      ex.printStackTrace(new PrintStream(new SystemOut("System.err")));
    }
    ///
    startGame();
  }

  public void startGame() {
    int maxNr = 100; //Maximum
    theNumber = 1 + (int) (maxNr * Math.random());
    countGuess = 0;
    tv2.setText(R.string.tv2);
    android.widget.Toast.makeText(getApplicationContext(), R.string.startStr, android.widget.Toast.LENGTH_SHORT).show();
  }

  public void checkTheNumber() {
    int guess = 0;
    try {
      Editable foo = edit1.getText();
      guess = Integer.parseInt(foo.toString());
    } catch (Exception e) {
      android.widget.Toast.makeText(getApplicationContext(), R.string.nonumber, android.widget.Toast.LENGTH_SHORT).show();
      e.printStackTrace();
    }
    edit1.setText("");
    /////
    System.out.println("theNumber: " + theNumber + " guess:" + guess);
    /////
    if (guess == theNumber) {
      //CORRECT
      addStrToTv2(R.string.correct, guess);
      android.widget.Toast.makeText(getApplicationContext(), R.string.win, android.widget.Toast.LENGTH_LONG).show();
    } else if (guess > theNumber) {
      //Lower
      addStrToTv2(R.string.lower, guess);
    } else if (guess < theNumber) {
      //Higher
      addStrToTv2(R.string.higher, guess);
    }

  }

  public void addStrToTv2(int t, int nr) {
    String a = (String) tv2.getText();
    a = getString(t) + " (" + nr + ")\n" + tv2.getText();
    tv2.setText(a);
  }
}
